package com.ruoyi.mp.controller;

import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.mp.service.IMpAccountService;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.invoice.merchant.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <h3>springcloudSaas</h3>
 * <p>商户电子发票相关的接口</p>
 *
 * @author : 郎兴宇
 * @date : 2024-10-07 18:42
 **/
@RestController
@RequestMapping("/merchant")
public class MpMerchantInvoiceController extends BaseController {
    @Autowired
    private IMpAccountService iMpAccountService;
    @Autowired
    private WxMpService wxService;


    /**
     * 发票冲红
     */
    @RequiresPermissions("mp:merchant:clearOutInvoice")
    @Log(title = "发票冲红.", businessType = BusinessType.INSERT)
    @PostMapping("/clearOutInvoice/{id}")
    public AjaxResult clearOutInvoice(@PathVariable Long id) throws WxErrorException {
        String appId = this.iMpAccountService.selectMpAccountById(id).getAppId();
        ClearOutInvoiceRequest params = new ClearOutInvoiceRequest();
        wxService.switchoverTo(appId).getMerchantInvoiceService().clearOutInvoice(params);
        return AjaxResult.success();
    }


    /**
     * 获得用户授权数据
     */
    @RequiresPermissions("mp:merchant:getAuthData")
    @Log(title = "获得用户授权数据.", businessType = BusinessType.EXPORT)
    @GetMapping("/getAuthData/{id}")
    public AjaxResult getAuthData(@PathVariable Long id) throws WxErrorException {
        String appId = this.iMpAccountService.selectMpAccountById(id).getAppId();
        InvoiceAuthDataRequest params =  new InvoiceAuthDataRequest();
        InvoiceAuthDataResult invoiceAuthDataResult =    wxService.switchoverTo(appId).getMerchantInvoiceService().getAuthData(params);
        return AjaxResult.success(invoiceAuthDataResult);
    }



    /**
     * 获取授权页面配置
     */
    @RequiresPermissions("mp:merchant:getAuthPageSetting")
    @Log(title = "获取授权页面配置.", businessType = BusinessType.EXPORT)
    @GetMapping("/getAuthPageSetting/{id}")
    public AjaxResult getAuthPageSetting(@PathVariable Long id) throws WxErrorException {
        String appId = this.iMpAccountService.selectMpAccountById(id).getAppId();
        InvoiceAuthPageSetting invoiceAuthPageSetting =    wxService.switchoverTo(appId).getMerchantInvoiceService().getAuthPageSetting();
        return AjaxResult.success(invoiceAuthPageSetting);
    }



    /**
     * 获取开票授权页链接
     */
    @RequiresPermissions("mp:merchant:getAuthPageUrl")
    @Log(title = "获取开票授权页链接.", businessType = BusinessType.EXPORT)
    @GetMapping("/getAuthPageUrl/{id}")
    public AjaxResult getAuthPageUrl(@PathVariable Long id) throws WxErrorException {
        String appId = this.iMpAccountService.selectMpAccountById(id).getAppId();
        InvoiceAuthPageRequest params = new InvoiceAuthPageRequest();
        InvoiceAuthPageResult invoiceAuthPageResult =    wxService.switchoverTo(appId).getMerchantInvoiceService().getAuthPageUrl(params);
        return AjaxResult.success(invoiceAuthPageResult);
    }



    /**
     * 获取商户联系方式
     */
    @RequiresPermissions("mp:merchant:getMerchantContactInfo")
    @Log(title = "获取商户联系方式.", businessType = BusinessType.EXPORT)
    @GetMapping("/getMerchantContactInfo/{id}")
    public AjaxResult getMerchantContactInfo(@PathVariable Long id) throws WxErrorException {
        String appId = this.iMpAccountService.selectMpAccountById(id).getAppId();

        MerchantContactInfo merchantContactInfo =    wxService.switchoverTo(appId).getMerchantInvoiceService().getMerchantContactInfo();
        return AjaxResult.success(merchantContactInfo);
    }


    /**
     * 获取商户开票平台信息
     */
    @RequiresPermissions("mp:merchant:getMerchantInvoicePlatform")
    @Log(title = "获取商户开票平台信息.", businessType = BusinessType.EXPORT)
    @GetMapping("/getMerchantInvoicePlatform/{id}")
    public AjaxResult getMerchantInvoicePlatform(@PathVariable Long id) throws WxErrorException {
        String appId = this.iMpAccountService.selectMpAccountById(id).getAppId();
        MerchantInvoicePlatformInfo merchantInvoicePlatformInfo =  new MerchantInvoicePlatformInfo();
        MerchantInvoicePlatformInfo merchantInvoicePlatformInfo1 =    wxService.switchoverTo(appId).getMerchantInvoiceService().getMerchantInvoicePlatform(merchantInvoicePlatformInfo);
        return AjaxResult.success(merchantInvoicePlatformInfo1);
    }


    /**
     * 开具电子发票
     */
    @RequiresPermissions("mp:merchant:makeOutInvoice")
    @Log(title = "开具电子发票.", businessType = BusinessType.EXPORT)
    @GetMapping("/makeOutInvoice/{id}")
    public AjaxResult makeOutInvoice(@PathVariable Long id) throws WxErrorException {
        String appId = this.iMpAccountService.selectMpAccountById(id).getAppId();
        MakeOutInvoiceRequest params = new MakeOutInvoiceRequest();
        wxService.switchoverTo(appId).getMerchantInvoiceService().makeOutInvoice(params);
        return AjaxResult.success();
    }


    /**
     * 查询发票信息
     */
    @RequiresPermissions("mp:merchant:queryInvoiceInfo")
    @Log(title = "查询发票信息.", businessType = BusinessType.EXPORT)
    @GetMapping("/queryInvoiceInfo/{id}")
    public AjaxResult queryInvoiceInfo(@PathVariable Long id) throws WxErrorException {
        String appId = this.iMpAccountService.selectMpAccountById(id).getAppId();
        String fpqqlsh = "123";
        String nsrsbh = "123";
        InvoiceResult  invoiceResult =      wxService.switchoverTo(appId).getMerchantInvoiceService().queryInvoiceInfo(fpqqlsh, nsrsbh);
        return AjaxResult.success(invoiceResult);
    }


    /**
     * 拒绝开票 场景: 用户授权填写数据无效 结果: 用户会收到一条开票失败提示
     */
    @RequiresPermissions("mp:merchant:rejectInvoice")
    @Log(title = "拒绝开票 场景: 用户授权填写数据无效 结果: 用户会收到一条开票失败提示.", businessType = BusinessType.EXPORT)
    @GetMapping("/rejectInvoice/{id}")
    public AjaxResult rejectInvoice(@PathVariable Long id) throws WxErrorException {
        String appId = this.iMpAccountService.selectMpAccountById(id).getAppId();
        InvoiceRejectRequest params = new InvoiceRejectRequest();
        wxService.switchoverTo(appId).getMerchantInvoiceService().rejectInvoice(params);
        return AjaxResult.success();
    }


    /**
     * 配置授权页面字段
     */
    @RequiresPermissions("mp:merchant:setAuthPageSetting")
    @Log(title = "配置授权页面字段.", businessType = BusinessType.EXPORT)
    @PostMapping("/setAuthPageSetting/{id}")
    public AjaxResult setAuthPageSetting(@PathVariable Long id) throws WxErrorException {
        String appId = this.iMpAccountService.selectMpAccountById(id).getAppId();
        InvoiceAuthPageSetting authPageSetting = new InvoiceAuthPageSetting();
        wxService.switchoverTo(appId).getMerchantInvoiceService().setAuthPageSetting(authPageSetting);
        return AjaxResult.success();
    }


    /**
     * 设置商户联系方式, 获取授权链接前需要设置商户联系信息
     */
    @RequiresPermissions("mp:merchant:setMerchantContactInfo")
    @Log(title = "设置商户联系方式, 获取授权链接前需要设置商户联系信息.", businessType = BusinessType.EXPORT)
    @PostMapping("/setMerchantContactInfo/{id}")
    public AjaxResult setMerchantContactInfo(@PathVariable Long id) throws WxErrorException {
        String appId = this.iMpAccountService.selectMpAccountById(id).getAppId();
        MerchantContactInfo contact = new MerchantContactInfo();
        wxService.switchoverTo(appId).getMerchantInvoiceService().setMerchantContactInfo(contact);
        return AjaxResult.success();
    }


    /**
     * 设置商户开票平台信息
     */
    @RequiresPermissions("mp:merchant:setMerchantInvoicePlatform")
    @Log(title = "设置商户开票平台信息.", businessType = BusinessType.EXPORT)
    @PostMapping("/setMerchantInvoicePlatform/{id}")
    public AjaxResult setMerchantInvoicePlatform(@PathVariable Long id) throws WxErrorException {
        String appId = this.iMpAccountService.selectMpAccountById(id).getAppId();
        MerchantInvoicePlatformInfo merchantInvoicePlatformInfo =  new MerchantInvoicePlatformInfo();
        wxService.switchoverTo(appId).getMerchantInvoiceService().setMerchantInvoicePlatform(merchantInvoicePlatformInfo);
        return AjaxResult.success();
    }
}


